package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ProductionOrderProductDao;
import com.xbongbong.saas.domain.entity.BusinessProductEntity;
import com.xbongbong.saas.domain.entity.ProductionOrderProductEntity;
import com.xbongbong.saas.model.ProductionOrderProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/2/21 14:33
 * @since v1.0
 */
@Service("productionOrderProductModel")
public class ProductionOrderProductModelImpl implements ProductionOrderProductModel {
    @Resource
    private ProductionOrderProductDao productionOrderProductDao;

    @Override
    public Integer insert(ProductionOrderProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        return productionOrderProductDao.insert(entity);
    }

    @Override
    public void inserBatch(List<ProductionOrderProductEntity> insetBatchList) throws XbbException{
        if (CollectionsUtil.isEmpty(insetBatchList)) {
            return;
        }
        StandardizeDataUtil.standardize4ProductionOrderProductEntityList(insetBatchList);
        productionOrderProductDao.insertBatch(insetBatchList);
    }

    @Override
    public Integer updateBatch(List<ProductionOrderProductEntity> list, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ProductionOrderProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4ProductionOrderProductEntityList(list);
        return productionOrderProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer updateOutstockBatch(List<ProductionOrderProductEntity> list, String corpid) throws XbbException {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ProductionOrderProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        return productionOrderProductDao.updateOutstockBatch(list, corpid);
    }

    @Override
    public Integer update(ProductionOrderProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        entity.setUpdateTime(DateTimeUtil.getInt());
        return productionOrderProductDao.update(entity);
    }

    @Override
    public List<ProductionOrderProductEntity> findEntitys(Map<String, Object> param) {
        return productionOrderProductDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return productionOrderProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public ProductionOrderProductEntity getByKey(Long key, String corpid) {
        return productionOrderProductDao.getByKey(key, corpid);
    }

    @Override
    public Integer save(ProductionOrderProductEntity productionOrderProductEntity) throws XbbException {
        if (productionOrderProductEntity.getId() == null || Objects.equals(0L, productionOrderProductEntity.getId())) {
            productionOrderProductEntity.setId(null);
            return productionOrderProductDao.insert(productionOrderProductEntity);
        }
        return productionOrderProductDao.update(productionOrderProductEntity);
    }

    @Override
    public List<ProductionOrderProductEntity> getProductsById(Long productionOrderId, String corpid, Integer productType) {
        // 获取bom单关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("del", 0);
        params.put("productionOrderId", productionOrderId);
        params.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        if (productType != null) {
            params.put("type", productType);
        }
        return findEntitys(params);
    }

    @Override
    public List<ProductionOrderProductEntity> getProductsById(Long productionOrderId, String corpid, Integer productType, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("delIn", delIn);
        params.put("productionOrderId", productionOrderId);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        if (productType != null) {
            params.put("type", productType);
        }
        return findEntitys(params);
    }

    /**
     * 获取生产单关联的成品
     *
     * @param proOrderId 生产单id
     * @param corpid     公司id
     * @return 生产单产品
     */
    @Override
    public ProductionOrderProductEntity findFinishedProduct(Long proOrderId, String corpid) {
        ProductionOrderProductEntity finishedProduct = null;
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("productionOrderId", proOrderId);
        param.put("type", 1);
        List<ProductionOrderProductEntity> productionOrderProducts = findEntitys(param);
        if (productionOrderProducts != null && productionOrderProducts.size() > BasicConstant.ZERO) {
            finishedProduct = productionOrderProducts.get(0);
        }
        return finishedProduct;
    }

    @Override
    public Integer deleteByProOrderIdIn(List<Long> list, String corpid) {
        return productionOrderProductDao.deleteByProOrderIdIn(list, corpid);
    }

    @Override
    public Integer dataConsistencyUpdate(String corpid, Long productId, String productName, String specification, String productNo) {
        return productionOrderProductDao.dataConsistencyUpdate(corpid, productId, productName, specification, productNo);
    }

    @Override
    public List<BusinessProductEntity> findProductNum(Map<String, Object> param) {
        return productionOrderProductDao.findProductNum(param);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer type, Integer isArray) {
        return productionOrderProductDao.isProductSubAttrExist(attr, corpid, type, isArray);
    }

    @Override
    public List<String> getCorpids() {
        return productionOrderProductDao.getCorpids();
    }

    @Override
    public Integer findProductNumCount(Map<String, Object> param) {
        return productionOrderProductDao.findProductNumCount(param);
    }

    @Override
    public Double summaryByAttr(String field,String corpid, Long refId, List<Long> productIdIn,Integer type){
        return productionOrderProductDao.summaryByAttr(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn, Integer type) {
        return productionOrderProductDao.summaryByAttrFromOutSide(field,corpid,  refId, productIdIn,type);
    }

    @Override
    public Double calcuateByAttrFromOutSide(String field1, String field2, String corpid, Long refId, List<Long> productIdIn, Integer type) {
        return productionOrderProductDao.calcuateByAttrFromOutSide(field1, field2, corpid,  refId, productIdIn,type);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer type, Integer isArray) {
        return productionOrderProductDao.countProductSub(attr, corpid, type, isArray);
    }

    @Override
    public List<ProductionOrderProductEntity> getProductWithSubData(String attr, String corpid, Integer type, Integer isArray, Long idGte, Integer limit) {
        return productionOrderProductDao.getProductWithSubData(attr, corpid, type, isArray, idGte, limit);
    }

    @Override
    public Integer deleteBatch(List<Long> idIn, String corpid, Integer del) {
        if (CollectionsUtil.isEmpty(idIn)) {
            return 0;
        }
        return productionOrderProductDao.deleteBatch(idIn, corpid, del);
    }

    @Override
    public Integer updateMulti(List<ProductionOrderProductEntity> list, String corpid) {
        if (CollectionsUtil.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        for (ProductionOrderProductEntity entity : list) {
            entity.setUpdateTime(now);
        }
        StandardizeDataUtil.standardize4ProductionOrderProductEntityList(list);
        return productionOrderProductDao.updateMulti(list, corpid);
    }
}
